<template>
  <el-form class="query-container" ref="queryFormRef" :model="queryParams" :inline="true" @submit.native.prevent>
    <el-form-item label="操作人" prop="operateName">
      <el-input
          v-model="queryParams.OperateName"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
      />
    </el-form-item>
    <el-form-item label="操作时间" prop="operateTime">
      <el-date-picker
          v-model="queryParams.operateTime"
          value-format="YYYY-MM-DD"
          type="date"
          style="width: 200px;"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleQuery">搜索</el-button>
      <el-button @click="resetQuery">重置</el-button>
    </el-form-item>
  </el-form>

  <div class="inner-container">
    <el-row :gutter="10" style="margin-bottom:8px" v-if="$hasPermission('crm:system:operateLog:del')" >
      <el-col :span="1.5" >
        <el-button type="danger" plain
                   @click="deleteData(null)"
                   :disabled="!selectedIds.length"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"/>
      <el-table-column label="序号" width="80">
        <template #default="scope">
          {{ (queryParams.pageNum - 1) * (queryParams.pageSize) + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="operateName" label="操作人"/>
      <el-table-column prop="operateIp" label="IP" show-overflow-tooltip/>
      <el-table-column prop="operateModel" label="操作模块" />
      <el-table-column prop="operateContent" label="操作内容"/>
      <el-table-column prop="operateTime" label="操作时间" show-overflow-tooltip/>
      <el-table-column prop="operateStatus" label="操作状态" show-overflow-tooltip/>
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="scope">
          <el-button type="primary" link icon="View" @click="showDialog(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>
    <el-pagination
        :total="total"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />

    <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="closeDialog" append-to-body>
      <el-form ref="dialogFormRef" :model="formData" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="操作人：" prop="operateName">
              {{ formData.operateName }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="IP：" prop="operateIp">
              {{ formData.operateIp }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作模块：" prop="operateModel">
              {{ formData.operateModel }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作内容：" prop="operateContent">
              <span style="word-break: break-all;">{{ formData.operateContent }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作时间：" prop="operateTime">
              {{ formData.operateTime }}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作状态：" prop="operateStatus">
              {{ formData.operateStatus }}
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <template #footer class="dialog-footer">
        <el-button type="primary" @click="closeDialog">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import request from '@/utils/request'
import {ElMessage} from "element-plus";

// --------------------------------------------------
const loading = ref(false)
const queryFormRef = ref()
const queryParams = ref({pageNum: 1, pageSize: 10,OperateName:'',operateTime:''})
const total = ref(0)
const tableData = ref([])
const selectedIds = ref([])

const listData = () => {
  loading.value = true
  let data = {...queryParams.value}
  console.log(data);
  request.get('/api/crm/operateLog/select', {
    params: data
  }).then(res => {
    total.value = res.data.total;
    tableData.value = res.data.list;
    loading.value = false
  });
}
const handleSelectionChange = (selection) => {
  selectedIds.value = selection.map(item => item.id)
}
const handleQuery = () => {
  queryParams.value.pageNum = 1
  listData()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleCurrentChange = (val) => {
  queryParams.value.pageNum = val
  listData()
}

const handleSizeChange = (val) => {
  queryParams.value.pageSize = val
  listData()
}

// --------------------------------------------------
const dialogTitle = ref('日志详情')
const dialogVisible = ref(false)
const dialogFormRef = ref()
const formData = ref({
  operateName:'',
  operateIp:'',
  operateModel:'',
  operateContent:'',
  operateTime:'',
  operateStatus:''
})
//查看详情
const showDialog = (row) => {
  let id = row.id
  request.get('/api/crm/operateLog/select', {
    params: {id:id}
  }).then(res => {
    formData.value = res.data.list[0];
    console.log(formData.value)
  });

  dialogVisible.value = true
}
//关闭弹窗
const closeDialog = () => {
  formData.value = {}
  dialogVisible.value = false
  listData()
}
//删除日志
const deleteData = (row) => {
  let ids = row ? [row.id] : [...selectedIds.value]
  console.log(ids)
  request.get(`/api/crm/operateLog/delete?ids=${ids}`).then(res => {
    ElMessage.success('删除成功')
    selectedIds.value.length = 0
    listData()
  });
}
// --------------------------------------------------
onMounted(() => {
  listData()
})
</script>

<style scoped>

</style>
